package com.example.authorization_server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.LinkedList;

/**
 * @author 罗俊华
 * @date 2022/3/10 - 4:19 下午
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        LinkedList<GrantedAuthority> grantedAuthorities = new LinkedList<>();

        if("luo".equals(username)){

            grantedAuthorities.add(new SimpleGrantedAuthority("createOrder"));
        }

        grantedAuthorities.add(new SimpleGrantedAuthority("viewOrder"));

        return new User(
                username,
                passwordEncoder.encode("1234"),
                grantedAuthorities
        );
    }
}
